// pages/normal/normal.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movies: [
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" }
    ],
    comments:[],
    num: 1,
    minusStatus: 'disable',
    _num: 1,
    display_evaluate: "none",
    display_collageWare: "block",
    display_details: "none",
    _number: 0,
    num_ber: 1,
    display_confirm: "none",
    collect: "inline-block",
    collect_select: "none",
    detail: '',
    iscode: '',
    Type: '',
    goodsid: ''
  },

  // 增加数量
  addCount:function(e) {
    console.log(e)
    var num = this.data.num;
    num++;
    this.setData({
      num: num,
    })      
  },
  // 减少数量
  minusCount:function(e) {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  focus_click: function (e) {
    var that = this
    this.setData({
      _num: e.target.dataset.num
    });
    if (e.target.dataset.num == 1) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "block",
        display_details: "none"
      })
    };
    if (e.target.dataset.num == 2) {
      this.setData({
        display_evaluate: "block",
        display_collageWare: "none",
        display_details: "none",
        comments: that.data.comments
      })


    };
    if (e.target.dataset.num == 3) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "none",
        display_details: "block"
      })
    };
  },
  has_focus_click: function (e) {
    let that=this;
   //console.log(e.currentTarget.dataset.index,'*------')
    //var list = that.data.
    this.setData({
      names: e.currentTarget.dataset.index,
      _number: e.currentTarget.dataset.id
    })
    console.log(that.data.detail.specification,'-------')
  },
  //减少数量
  minus: function () {
    var num_ber = this.data.num_ber;
    if (num_ber > 1) {
      num_ber--;
    }
    var minusStatus = num_ber > 1 ? 'normal' : 'disable';
    this.setData({
      num_ber: num_ber,
      minusStatus: minusStatus
    })
  },
  //增加数量
  add: function (e) {
    var num_ber = this.data.num_ber;
    num_ber++;
    var minusStatus = num_ber > 1 ? 'normal' : 'disable';
    this.setData({
      num_ber: num_ber,
      minusStatus: minusStatus
    })
  },

  buy_now:function(){
    this.setData({
      display_confirm:"block",
      Type:0
    })
  },
  buy_nows: function () {
    this.setData({
      display_confirm: "block",
      Type: 1
    })
  },
  hideview: function () {
    this.setData({
      display_confirm: "none"
    })
  },
  evaluate:function(){
    this.setData({
      display_evaluate: "block",
      display_collageWare: "none",
      display_details: "none"
    })
  },
  collect: function () {
    // this.setData({
    //   collect: "none",
    //   collect_select: "inline-block",
    //   collection: true
    // })
    let that = this
    var iscode = that.data.iscode;

    console.log(iscode, '-----------------');
    wx.request({
      url: app.data.Url + 'collect/addcollect',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        goodsid: that.data.detail.goodsid,
        status: iscode,
        type: 0
      },
      method: 'get',
      success: function (res) {
        console.log("-----", res)
        if (res.data.msg == '收藏成功') {
          that.setData({
            iscode: 1
          })
          wx.showToast({
            title: '收藏成功',
          })
        }
        if (res.data.msg == '取消收藏') {
          that.setData({
            iscode: 0
          })
          wx.showToast({
            title: '取消收藏',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    /***商品详情 */
    wx.request({
      url: app.data.Url+'goods/detail',
      data:{
        goodsid:options.id,
        // goodsid:427237
      },
      method:'get',
      success:function(res){
        var WxParse = require('../../wxParse/wxParse.js');
        var result = res.data.data.content;
        WxParse.wxParse("result", "html", result, that, 5)
        if (res.data.data.specification == undefined){
          that.setData({
            detail: res.data.data,
          })
        }else{
          that.setData({
            detail: res.data.data,
            cate: res.data.data.specification[0],
            names: res.data.data.specification[0]
          })
        }

      }
    })
    var goodsid = options.id;
    wx.request({
      url: app.data.Url + 'collect/iscollect',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        goodsid: goodsid
      },
      method: 'get',
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 0) {
          that.setData({
            iscode: 0
          })
        } else {
          that.setData({
            iscode: 1
          })
        }

      }
    })
    /**
     * 商品评价
     */
    wx.request({
      url: app.data.Url + 'comment/commentLists',
      data:{
        goodsid: options.id
      },
      method:'get',
      success:function(res){
        console.log('商品评论',res.data.data)
        if (res.data.data.length>0){
          that.setData({
            comment: res.data.data[0],
            comments: res.data.data,
            goodsid: options.id
          })
        }
        
      }
    })
  },

  click:function(){
    let that=this;
    var type = that.data.Type;
    var goodsid = that.data.detail.goodsid;
    var num = that.data.num
    var price = that.data.names.price;
 
    if (type == 0){
      app.navTo('../confirm/confirm?goodsid=' + goodsid + '&num=' + num + '&guige=' + that.data.names.rule+'&price='+price)
    }else{
      wx.request({
        url: app.data.Url +'cart/cartadd',
        data:{
          goodsid:goodsid,
          num:num,
          uid:wx.getStorageSync('userInfo').uid
        },
        method:'post',
        success:function(res){
          if (res.data.code==1){
            wx.showToast({
              title: '添加成功！',
            })
          }
        }
      })
    }

  },
  share: function () {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})